package com.zj.osm.utils;

import android.os.Environment;
import android.util.Log;
import android.util.Xml;

import com.zj.osm.App;
import com.zj.osm.entity.db.DaoSession;
import com.zj.osm.entity.db.LineDao;
import com.zj.osm.entity.db.PointDao;
import com.zj.osm.entity.greendao.Equipment;
import com.zj.osm.entity.greendao.Line;
import com.zj.osm.entity.greendao.Multimedia;
import com.zj.osm.entity.greendao.Point;
import com.zj.osm.entity.greendao.Project;
import com.zj.utils.utils.utilcode.ToastUtils;

import org.xmlpull.v1.XmlSerializer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author jiang zhu on 2019/10/29
 */
public class ExportUtils {

    private static final String ABSOLUTE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/bhne/";
    private static final String LINE_SEPARATOR = "line.separator";
    private static final String ENCODING = "UTF-8";
    private DaoSession daoSession = App.getInstances().getDaoSession();
    private List<Point> pointList = new ArrayList<>();
    private List<Line> lineList = new ArrayList<>();
    private List<Line> withTheLeverLineList = new ArrayList<>();

    /**
     * 获取项目的zip包
     *
     * @param project 项目信息
     * @return zip包
     */
    public File getZipFile(Project project) {
        if (project == null) {
            throw new NullPointerException("项目为空");
        }
        pointList = daoSession.getPointDao().queryBuilder().where(PointDao.Properties.ProjectId.eq(project.getId())).list();
        if (pointList.size() < 1) {
            ToastUtils.showShort("当前项目没有点信息");
            return null;
        }
        lineList = daoSession.getLineDao().queryBuilder().where(LineDao.Properties.ProjectId.eq(project.getId()), LineDao.Properties.ParallelPole.isNull()).list();
        withTheLeverLineList = daoSession.getLineDao().queryBuilder().where(LineDao.Properties.ProjectId.eq(project.getId()), LineDao.Properties.ParallelPole.eq("同杆并架")).list();
        File file = new File(ABSOLUTE_PATH + project.getName() + "/" + project.getName());
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }

        return createXML(project);
    }

    /**
     * 创建XML
     *
     * @param project 项目
     */
    private File createXML(Project project) {
        File xmlFile = new File(ABSOLUTE_PATH + project.getName() + "/" + project.getName(), project.getName() + project.getId() + ".xml");
        try {
            FileOutputStream file = new FileOutputStream(xmlFile);
            XmlSerializer serializer = Xml.newSerializer();
            serializer.setOutput(file, ENCODING);
            serializer.startDocument(null, true);
            changeLine(serializer);
            serializer.startTag(null, "Projects");
            changeLine(serializer);
            serializer.startTag(null, "ProjectId");
            serializer.text(String.valueOf(project.getProjectId()));
            serializer.endTag(null, "ProjectId");
            changeLine(serializer);
            serializer.startTag(null, "Voltage");
            serializer.text(String.valueOf(project.getVoltage()));
            serializer.endTag(null, "Voltage");
            changeLine(serializer);
            for (int i = 0; i < pointList.size(); i++) {
                serializer.startTag(null, "Point");
                serializer.attribute(null, "Name", pointList.get(i).getMark().trim());
                serializer.attribute(null, "Weidu", String.valueOf(pointList.get(i).getLatitude()));
                serializer.attribute(null, "Jindu", String.valueOf(pointList.get(i).getLongitude()));
                serializer.attribute(null, "Sbzt", String.valueOf(pointList.get(i).getState()));
                List<Equipment> equipmentList = pointList.get(i).getEquipmentList();
                for (int j = 0; j < equipmentList.size(); j++) {
                    serializer.startTag(null, "PointModel");
                    serializer.attribute(null, "Module_ID", equipmentList.get(i).getModuleID().trim());
                    serializer.endTag(null, "PointModel");
                }
                List<Multimedia> multimediaList = pointList.get(i).getMultimediaList();
                for (int j = 0; j < multimediaList.size(); j++) {
                    serializer.startTag(null, "Multimedia");
                    serializer.attribute(null, "Name", multimediaList.get(i).getName().trim());
                    serializer.attribute(null, "Path", multimediaList.get(i).getPath().trim());
                    serializer.endTag(null, "Multimedia");
                }
                serializer.endTag(null, "Point");
                changeLine(serializer);
            }


            for (int i = 0; i < lineList.size(); i++) {
                serializer.startTag(null, "Line");
                serializer.attribute(null, "LineName", String.valueOf(lineList.get(i).getName()));
                serializer.endTag(null, "Line");
                changeLine(serializer);
            }

            serializer.endTag(null, "Projects");
            changeLine(serializer);
            serializer.endDocument();
            serializer.flush();
            file.close();

        } catch (IOException e) {
            Log.e("error=", Objects.requireNonNull(e.getMessage()));
            e.printStackTrace();
        }
        return xmlFile;
    }


    /**
     * 为XML文件换行
     */
    private void changeLine(XmlSerializer serializer) {
        try {
            serializer.text(System.getProperty(LINE_SEPARATOR));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

}
